
<template>
  <rx-layout>
    <div slot="header" style="display: flow-root;">
        <div style="float: left">
            <a-switch v-model="isEnabledOption" />
            <div style="color: #ccc;font-size: 12px;">
                {{i18n('pwdForm.turnOn','开关打开')}}
            </div>
        </div>
        <a-button @click="saveConfig" icon="save" type="primary" style="float: right">{{$ti18('rxForm.saveBtn','保存')}}</a-button>
    </div>
    <div v-if="isEnabledOption" slot="center">
      <a-form-model class="password-form-box" ref="form" :model="mdl" :rules="rules" :label-col="labelCol1" :wrapper-col="wrapperCol">
        <a-row :span="24" type="flex" justify="space-around" align="middle">
          <a-col :span="12" class="has-tips">
            <a-form-model-item :label="i18n('pwdForm.minLength','最小密码长度')" prop="minLength">
                <a-input-number :min="1" :max="10" :placeholder="i18n('pwdForm.minLength','最小密码长度','input')" v-model="mdl.minLength" />
                <div style="color: #ccc;font-size: 12px;">{{i18n('pwdForm.minLengthDesc','设置值不能小于系统密码最小长度值，此项不填默认为1')}}</div>
            </a-form-model-item>

          </a-col>
            <a-col :span="12" class="has-tips">
                <a-form-model-item :label="i18n('pwdForm.maxLength','最大密码长度')" prop="minLength">
                    <a-input-number :min="1" :max="20" :placeholder="i18n('pwdForm.maxLength','最大密码长度','input')" v-model="mdl.maxLength" />
                    <div style="color: #ccc;font-size: 12px;">{{i18n('pwdForm.maxLengthDesc','设置值不能超过系统密码最大长度值')}}</div>
                </a-form-model-item>

            </a-col>

        </a-row>

        <a-row :span="24">

              <a-col :span="12" class="has-tips">
                  <a-form-model-item :label="i18n('pwdForm.isMixOption','密码构成')" prop="isMix">
                      <a-switch v-model="isMixOption" />
                      <div style="color: #ccc;font-size: 12px;">
                          {{i18n('pwdForm.isMixOptionDesc','开关打开，则密码必须有英文数字,可以有特殊字符')}}
                      </div>
                  </a-form-model-item>
              </a-col>
            <a-col :span="12">
                <a-form-model-item :label="i18n('pwdForm.isFit','账号和密码不能一致')" prop="isNeverTimeout">
                    <!-- <a-checkbox v-model="isUsernamePwdConsistentOption"></a-checkbox> -->
                    <a-switch v-model="isUsernamePwdConsistentOption" />
                </a-form-model-item>
            </a-col>
        </a-row>

        <a-row :span="24">

            <a-col :span="12" class="has-tips">
                <a-form-model-item :label="i18n('pwdForm.errorTime','最多错误输入密码次数')" prop="errorTime">
                    <a-input-number
                        :min="0"
                        :max="1000000"
                        :placeholder="i18n('pwdForm.errorTime','最多错误输入密码次数','input')"
                        v-model="mdl.errorTime"
                    />
                    <div style="color: #ccc;font-size: 12px;">{{i18n('pwdForm.errorTimeDesc','不填则不做此项检查')}}</div>
                </a-form-model-item>
            </a-col>
          <a-col :span="12">
            <a-form-model-item :label="i18n('pwdForm.accountLockDay','账号锁定天数')" prop="accountLockDay">
              <a-input-number :min="0" :max="1000000" :placeholder="i18n('pwdForm.accountLockDay','账号锁定天数','input')" v-model="mdl.accountLockDay" />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row :span="24">
          <a-col :span="12" class="has-tips">
            <a-form-model-item :label="i18n('pwdForm.isFirst','首次登录密码')" prop="isNeverTimeout">
              <a-switch v-model="isFirstLoginUpdateOption" />
              <div style="color: #ccc;font-size: 12px;">
                  {{i18n('pwdForm.isFirstDesc','开关打开，则用户首次登录系统必须修改密码')}}
              </div>
            </a-form-model-item>
          </a-col>
              <a-col :span="12" class="has-tips">
                  <a-form-model-item :label="i18n('pwdForm.isNeverTimeout','密码永不过期')" prop="isNeverTimeout">
                      <!-- <a-checkbox v-model="isNeverTimeoutOption"></a-checkbox> -->
                      <a-switch v-model="isNeverTimeoutOption" />
                      <div style="color: #ccc;font-size: 12px;">
                          {{i18n('pwdForm.isNeverTimeoutDesc','开关打开，则密码永不过期')}}
                      </div>
                  </a-form-model-item>

          </a-col>

        </a-row>

       <div v-show="!isNeverTimeoutOption">
            <a-row :span="24">
              <a-col :span="12">
                <a-form-model-item :label="i18n('pwdForm.timeoutDay','密码过期时间（天）')" prop="timeoutDay" >
                  <a-input-number  :min="0" :max="1000000" :placeholder="i18n('pwdForm.timeoutDay','密码过期时间（天）','input')" v-model="mdl.timeoutDay" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item :label="i18n('pwdForm.informDay','几天后通知修改密码（天）')" prop="informDay" >
                  <a-input-number
                      :min="0"
                    :max="1000000"
                    :placeholder="i18n('pwdForm.informDay','几天后通知修改密码（天）','input')"
                    v-model="mdl.informDay"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>

            <a-row :span="24">
              <a-col :span="12">
                <a-form-model-item :label="i18n('pwdForm.informType','密码修改通知方式')" prop="informType" >
                  <a-checkbox-group :options="msgOptions" v-model="informTypeOptions"></a-checkbox-group>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item :label="i18n('pwdForm.informContent','通知内容')" prop="informContent" >
                  <a-input :placeholder="i18n('pwdForm.informContent','通知内容','input')" v-model="mdl.informContent" />
                </a-form-model-item>
              </a-col>
            </a-row>

            <a-row :span="24">
              <a-col :span="12" class="has-tips">
                <a-form-model-item
                  :label="i18n('pwdForm.informFrequency','通知频率(天)')"
                  prop="informFrequency"
                >
                  <a-input-number
                    :min="0"
                    :max="1000000"
                    :placeholder="i18n('pwdForm.informFrequencyInput','通知频率(天)')"
                    v-model="mdl.informFrequency"
                  />
                  <div style="color: #ccc;font-size: 12px;">
                      {{i18n('pwdForm.informFrequencyDesc','设置每隔几天通知一次')}}
                  </div>
                </a-form-model-item>
              </a-col>
            </a-row>

          </div>


      </a-form-model>

    </div>
  </rx-layout>
</template>
<script>
import OsPasswordPolicyApi from '@/api/user/org/osPasswordPolicy'
import { RxDialog, BaseFormModel, RxUserComponent } from "@lowcode/jpaas-common-lib"

export default {
  name: 'OsPasswordPolicyEdit',
  mixins: [BaseFormModel],
  components: {
    RxDialog,
    RxUserComponent
  },
  data() {
    return {
      labelCol1: {span: 8},
      wrapperCol: {span: 12},
      isEnabledOption: true,
      isMixOption: false,
      isNeverTimeoutOption: true,
      isFirstLoginUpdateOption: false,
      isUsernamePwdConsistentOption: false,
      informTypeOptions: [],
      msgOptions: [
        { label: this.i18n('pwdForm.sms','短信'), value: 'sms' },
        { label: this.i18n('pwdForm.inner','消息中心'), value: 'inner' },
        { label: this.i18n('pwdForm.mail','电子邮箱'), value: 'mail' },
      ],
      rules: {
        minLength: [{ required: false, message: this.i18n('pwdForm.minLength','最小密码长度','input'), trigger: 'change' }],
        errorTime: [{ required: false, message: this.i18n('pwdForm.errorTime','错误输入密码次数','input'), trigger: 'change' }],
        isMix: [{ required: false, message: this.i18n('pwdForm.pwdConsisting','请输入密码同时由英文大小写特殊字符和数字构成','input'), trigger: 'change' }],
        isNeverTimeout: [{ required: false, message: this.i18n('pwdForm.isNeverTimeout','密码永不过期','select'), trigger: 'change' }],
        timeoutDay: [{ required: false, message: this.i18n('pwdForm.timeoutDay','密码过期时间（天）','input'), trigger: 'change' }],
        informDay: [{ required: false, message: this.i18n('pwdForm.informDay','几天后通知修改密码（天）','input'), trigger: 'change' }],
        informType: [{ required: false, message: this.i18n('pwdForm.informType','密码修改通知方式','input'), trigger: 'change' }],
        informContent: [{ required: false, message: this.i18n('pwdForm.informContent','通知内容','input'), trigger: 'change' }],
      },
    }
  },
  created() {
    this.getConfig()
  },
  methods: {
    i18n(name,text,key){
        return this.$ti18(name,text,"SysConfig",key);
    },
    getConfig() {
        let self=this;
      OsPasswordPolicyApi.getPasswordPolicy().then((res) => {
        if (res.success) {
          if (res.data) {
              self.mdl = res.data
              self.mdl.isEnabled == 'YES' ? (self.isEnabledOption = true) : (self.isEnabledOption = false)
              self.mdl.isMix == 'YES' ? (self.isMixOption = true) : (self.isMixOption = false)
              self.mdl.isNeverTimeout == 'YES' ? (self.isNeverTimeoutOption = true) : (self.isNeverTimeoutOption = false)
              self.mdl.isFirstLoginUpdate == 'YES'
              ? (self.isFirstLoginUpdateOption = true)
              : (self.isFirstLoginUpdateOption = false)
              self.mdl.isUsernamePwdConsistent == 'YES'
              ? (self.isUsernamePwdConsistentOption = true)
              : (self.isUsernamePwdConsistentOption = false)
              if(self.mdl.informType){
                  self.informTypeOptions = self.mdl.informType.split(',')
              }
          }
        }
      })
    },
    saveConfig() {
      if (!this.isNeverTimeoutOption && this.mdl.informDay > this.mdl.timeoutDay) {
        this.$message.warning(this.i18n('msa.informDayTips','几天后通知修改密码（天）的值不能大于密码过期时间（天）的值！'))
        return
      }
      if (this.informTypeOptions.length > 0) {
        this.mdl.informType = this.informTypeOptions.join(',')
      } else {
        this.mdl.informType = ''
      }

      this.isEnabledOption ? (this.mdl.isEnabled = 'YES') : (this.mdl.isEnabled = 'NO')
      this.isMixOption ? (this.mdl.isMix = 'YES') : (this.mdl.isMix = 'NO')
      this.isNeverTimeoutOption ? (this.mdl.isNeverTimeout = 'YES') : (this.mdl.isNeverTimeout = 'NO')
      this.isFirstLoginUpdateOption ? (this.mdl.isFirstLoginUpdate = 'YES') : (this.mdl.isFirstLoginUpdate = 'NO')
      this.isUsernamePwdConsistentOption
        ? (this.mdl.isUsernamePwdConsistent = 'YES')
        : (this.mdl.isUsernamePwdConsistent = 'NO')
      OsPasswordPolicyApi.saveOrUpdateConfig(this.mdl)
    },
  },
}
</script>
<style scoped>
.relick a {
  margin-right: 10px;
}

.password-form-box >>> .has-tips {
    /* margin-top: -22px; */
}
.password-form-box >>> .has-tips .ant-form-item-control {
    line-height: 22px;
    margin-top: 4px;
}
.password-form-box >>> .tips{
    color: #ccc;
    font-size: 12px;
}
.password-form-box>>>.ant-input-number{
    width: 100%;
}
</style>
